import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hockey/extensions/jzx_json_extension.dart';
part 'jzx_config_data.freezed.dart';
part 'jzx_config_data.g.dart';

@freezed
class JzxConfigData with _$JzxConfigData {
  const factory JzxConfigData({
    @JzxJsonAlwaysString() @Default('') String rvsta,
    @JzxJsonAlwaysString() @Default('') String ver,
    @Default([]) List<ConfigItems> items,
    RiskControlInfoConfig? riskControlInfoConfig,
  }) = _JzxConfigData;

  factory JzxConfigData.fromJson(Map<String, Object?> json) =>
      _$JzxConfigDataFromJson(json);
}

@freezed
class ConfigItems with _$ConfigItems {
  const factory ConfigItems({
    @JzxJsonAlwaysString() @Default('') String data,
    @JzxJsonAlwaysString() @Default('') String name,
  }) = _ConfigItems;

  factory ConfigItems.fromJson(Map<String, Object?> json) =>
      _$ConfigItemsFromJson(json);
}

@freezed
class RiskControlInfoConfig with _$RiskControlInfoConfig {
  const factory RiskControlInfoConfig({
    int? kInterval,
    @JzxJsonAlwaysString() @Default('') String kFactor,
    @JzxJsonAlwaysString() @Default('') String kFactorNum,
  }) = _RiskControlInfoConfig;

  factory RiskControlInfoConfig.fromJson(Map<String, Object?> json) =>
      _$RiskControlInfoConfigFromJson(json);
}

extension JzxConfigDataExt on JzxConfigData {
  /// google_translation_key
  String get googleTranslationKey {
    final temp = items.firstWhere(
        (element) => element.name == 'google_translation_key',
        orElse: () => const ConfigItems());
    return temp.data;
  }

  /// rc_app_key
  String get rcAppKey {
    final temp = items.firstWhere((element) => element.name == 'rck',
        orElse: () => const ConfigItems());
    return temp.data;
  }
}
